
import { UI_Layer, UI_Listen } from '../../config/ui_def'
import scrollOpt from '../base/scrollOptimize'
import { WX_AD_CONFIG, Sdk_Platform } from '../../frame/constants';
cc.Class({
    extends: scrollOpt,

    properties: {
        topAd_node: cc.Node,
        randomBtn: cc.Node,
        goOnBtn: cc.Node
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start() {

    },

    // update (dt) {},

    initPage() {

    },
    updatePage() {
        this.randomBtn.active = true
        this.goOnBtn.active = false
        this.adSwitch(true)
        this.scheduleOnce(() => {
            this.randomBtn.active = false
            this.goOnBtn.active = true
        }, 2.5)
    },
    closeUI() {
        for (let i = (this.topAd_node.children.length); i > 0; i--) {
            this.topAd_node.children[i - 1].getComponent('scrollAd').clearItem()
            he.pool.putElement('scrollAd', this.topAd_node.children[i - 1])
        }
        for (let j = (this.content.children.length); j > 0; j--) {
            this.content.children[j - 1].getComponent('adIcon').clear()
            he.pool.putElement('adIcon', this.content.children[j - 1])
        }
    },

    adSwitch(status) {
        if (status) {
            switch (common.sdk_env) {
                case Sdk_Platform.WECHAT:
                    he.ad.getTdAdJson(WX_AD_CONFIG.tmIcon[11], this.uiSet.bind(this))
                    he.ad.getTdAdJson(WX_AD_CONFIG.tmIcon[12], this.gridInit.bind(this))
                    break
            }
        } else {
            //  关掉广告
        }
    },

    uiSet(config) {
        if (config.isOpen) {
            let pre = he.pool.getElement('scrollAd')
            pre.parent = this.topAd_node
            pre.getComponent('scrollAd').init(config.creatives)
        }
    },

    gridInit(config) {
        if (config.isOpen) {
            for (let i = 0; i < config.creatives.length; i++) {
                let pre = he.pool.getElement('adIcon')
                pre.position = cc.Vec2.ZERO
                pre.opacity = 255
                pre.parent = this.content
                let spt = pre.getComponent('adIcon')
                spt.init(config.creatives[i], 1, -1, 1.15)
                if (i == 0) {
                    this.posId = config.creatives[i].positionId
                    this.crtId = config.creatives[i].creativeId
                }
            }
        }
    },

    randomServer() {
        if (wx.tmSDK) {
            wx.tmSDK.flowNavigate({
                positionId: this.posId, // 广告位id, 请先使用该id获取推广创意列表
                creativeId: +this.crtId,  // 传入获取到的creativeId
            }).then((newList) => {
                // console.log('跳转成功or取消跳转（可根据特有返回值区分两种情况）')
                console.log('自动刷新列表：', newList)//返回最新列表 
                this.randomBtn.active = false
                this.goOnBtn.active = true
            }).catch((error) => {
                console.log('跳转失败', error);
                this.randomBtn.active = false
                this.goOnBtn.active = true
            })
        }
    },

    goOnServer() {
        let ver_info = he.local.getJson('ver_info')
        if (ver_info.fake_game_list) {
            gg.uiManager.addPage(UI_Layer.Load, {
                name: "fakeRan",
            })
        } else {
            gg.uiManager.addPage(UI_Layer.Load, {
                name: "LoadCover",
                in: true,
                scene: 'Main'
            })
        }
    },


});
